/**
  ******************************************************************************
  * File Name          : sysrtc.c
  * Description        : This file provides code for the configuration
  *                      of the RTC instances.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "bsp.h"
																						
Sys_DataTimeType Sys_Runtime;
Sys_DataTimeType Sys_Adjtime;

/**
  * @brief  Converts a 2 digit decimal to BCD format.
  * @param  Value Byte to be converted
  * @retval Converted byte
  */
uint8_t byte_to_bcd(uint8_t Value)
{
  uint32_t bcdhigh = 0U;

  while(Value >= 10U)
  {
    bcdhigh++;
    Value -= 10U;
  }

  return  ((uint8_t)(bcdhigh << 4U) | Value);
}

/**
  * @brief  Converts from 2 digit BCD to Binary.
  * @param  Value BCD value to be converted
  * @retval Converted word
  */
uint8_t bcd_to_byte(uint8_t Value)
{
  uint32_t tmp = 0U;
  tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
  return (tmp + (Value & (uint8_t)0x0F));
}

/* RTC init function */
void SystemRTC_Config(void)
{
	rtc_parameter_struct rtc_initpara;
	
	rcu_periph_clock_enable(RCU_PMU);
	pmu_backup_write_enable();

  rcu_osci_on(RCU_LXTAL);
	rcu_osci_stab_wait(RCU_LXTAL);
	rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
	
	rcu_periph_clock_enable(RCU_RTC);
	rtc_register_sync_wait();
	
  if(0x33776 != RTC_BKP0)
  {
    /* Initialize RTC and set the Time and Date */
    rtc_initpara.factor_asyn = 0x7Fu;
    rtc_initpara.factor_syn = 0xFFu;
    rtc_initpara.year = 0x21u;
    rtc_initpara.day_of_week = RTC_THURSDAY;
    rtc_initpara.month = RTC_APR;
    rtc_initpara.date = 0x16u;
    rtc_initpara.display_format = RTC_24HOUR;
    rtc_initpara.am_pm = RTC_AM;
    rtc_initpara.hour = 0x10u;
    rtc_initpara.minute = 0x00u;
    rtc_initpara.second = 0x00u;
    rtc_init(&rtc_initpara);
    
    RTC_BKP0 = 0x33776;
    
    _printf("RTC Init!");
  }
}

void Sys_GetDataTime(Sys_DataTimeType *dt)
{
	uint32_t temp_tr = 0U, temp_dr = 0U;
  
	temp_tr = (uint32_t)RTC_TIME;   
	temp_dr = (uint32_t)RTC_DATE;

	dt->year  = bcd_to_byte( (uint8_t)GET_DATE_YR(temp_dr) );
	dt->month = bcd_to_byte( (uint8_t)GET_DATE_MON(temp_dr) );
	dt->day   = bcd_to_byte( (uint8_t)GET_DATE_DAY(temp_dr) );
	
	dt->hour   = bcd_to_byte( (uint8_t)GET_TIME_HR(temp_tr) );
	dt->minute = bcd_to_byte( (uint8_t)GET_TIME_MN(temp_tr) );
	dt->second = bcd_to_byte( (uint8_t)GET_TIME_SC(temp_tr) );
}

void Sys_SetDataTime(Sys_DataTimeType *dt)
{
	uint32_t reg_time = 0u, reg_date = 0u;

	reg_date = (DATE_YR(byte_to_bcd(dt->year)) | \
							DATE_MON(byte_to_bcd(dt->month)) | \
							DATE_DAY(byte_to_bcd(dt->day))); 
	
	reg_time = (TIME_HR(byte_to_bcd(dt->hour))  | \
							TIME_MN(byte_to_bcd(dt->minute)) | \
							TIME_SC(byte_to_bcd(dt->second))); 
						
	/* 1st: disable the write protection */
	RTC_WPK = RTC_UNLOCK_KEY1;
	RTC_WPK = RTC_UNLOCK_KEY2;

	/* 2nd: enter init mode */
	if(rtc_init_mode_enter() != ERROR)
	{
		RTC_TIME = (uint32_t)reg_time;
		RTC_DATE = (uint32_t)reg_date;
		
		/* 3rd: exit init mode */
		rtc_init_mode_exit();
		
		/* 4th: wait the RSYNF flag to set */
		rtc_register_sync_wait();
	}
	
	/* 5th:  enable the write protection */
	RTC_WPK = RTC_LOCK_KEY;

}

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
